<template>
  <div class="user">
    <basic-container>
      <avue-crud
        :option="option"
        ref="crud"
        v-model="form"
        :page="page"
        :simple-page="true"
        @size-change="sizeChange"
        @current-change="currentChange"
        :table-loading="listLoading"
        @selection-change="selectionChange"
        @search-change="handleFilter"
        @refresh-change="resetHandleFilter"
        @row-update="update"
        @search-reset="resetHandleFilter"
        @row-save="create"
        :before-open="handleOpenBefore"
        :data="list"
      >
        <template slot="menuLeft">
          <el-button
            class="filter-item"
            @click="$refs.crud.rowAdd()"
            type="primary"
            icon="el-icon-edit"
            >添加标签
          </el-button>
          <el-button
            class="filter-item"
            @click="checkNum"
            type="primary"
            icon="el-icon-s-operation"
            >数量校准
          </el-button>
        </template>

        <template slot="menu" slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, scope.index)"
            >编辑
          </el-button>
          <!-- <el-button
            type="text"
            icon="el-icon-view"
            @click="handleview(scope.row, scope.index)"
            >查看详情
          </el-button> -->
          <!-- <el-button
            type="text"
            icon="el-icon-delete"
            @click="deletes(scope.row, scope.index)"
            >删除
          </el-button> -->
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  putTag,
  updateService,
  checkTagNum
} from "@/api/admin/tags";
import { tableOption } from "@/const/crud/admin/tags";
import { mapGetters } from "vuex";

export default {
  name: "table_user",
  data() {
    return {
      option: tableOption,
      params: {},
      queryList: {},
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false //是否倒序
      },
      list: [],
      listLoading: true,
      role: [],
      form: {}
    };
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  watch: {
    role() {
      this.form.role = this.role;
    }
  },
  created() {
    this.getList(this.page, {});
    // this.sys_user_add = this.permissions["sys_user_add"];
    // this.sys_user_edit = this.permissions["sys_user_edit"];
    // this.sys_user_del = this.permissions["sys_user_del"];
  },
  methods: {
    getList(page, params) {
      this.listLoading = true;
      fetchList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          params || this.params
        )
      ).then(response => {
        this.list = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.listLoading = false;
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    handleFilter(param, done) {
      this.params = param;
      this.page.currentPage = 1;
      this.getList(this.page, param);
      done();
    },
    resetHandleFilter() {
      this.params = null;
      this.page.currentPage = 1;
      this.getList(this.page);
    },
    handleRefreshChange() {
      this.getList();
    },
    handleOpenBefore(show, type) {
      show();
    },
    handleClose(done) {
      done();
    },
    handleview(data, index) {
      this.$refs.crud.rowView(data, index);
    },
    changeFlag(status, id) {
      let num;
      if (status === 0) {
        num = 1;
      } else {
        num = 0;
      }
      updateService({ status: num, id: id }).then(res => {
        this.getList(this.page);
        this.$notify.success("操作成功");
      });
    },
    selectionChange(list) {
      this.selIds = (list || []).map(o => o.id).join(",");
    },
    checkNum() {
      this.listLoading = true
      checkTagNum()
        .then(() => {
          this.listLoading = false
          this.getList(this.page);
        })
    },
    handleUpdate(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    create(row, done, loading) {
      delete this.form.create_time;

      putTag(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("创建成功");
        })
        .catch(() => {
          loading();
        });
    },
    update(row, index, done, loading) {
      delete this.form.create_time;

      putTag(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("修改成功");
        })
        .catch(() => {
          loading();
        });
    },
    deletes(row) {
      this.$confirm("此操作将永久删除该标签, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteCourse({ id: row.id })
          .then(() => {
            this.getList(this.page);
            this.$notify.success("删除成功");
          })
          .catch(() => {
            this.$notify.error("删除失败");
          });
      });
    }
  }
};
</script>
